package com.rural.bus;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.rural.bus.adapter.OrderAdapter;
import com.rural.bus.model.Order;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.utils.ProgressUtil;
import com.rural.bus.view.xlist.XListView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

/***
 * 未完成订单
 * @author majie
 *
 */
public class InServiceActivity extends BaseActivity {
	private XListView mXlv;
	private List<Order> mOrders=new ArrayList<Order>();
	private OrderAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orderlist);
		
		initView();
		setListeners();
		initData();
	}

	@Override
	protected void initView() {
		mBtMiddle.setText("未完成订单");
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);
		
		mXlv=(XListView) findViewById(R.id.xlv);
		mXlv.setPullRefreshEnable(false);
	}

	@Override
	protected void initData() {
		if(mCurrentUser==null){
			startActivity(LoginActivity.class);
			finish();
			return;
		}
		getData();
	}
	
	private void getData(){
		ProgressUtil.showProgress(this, "");
		//获取服务中的订单
		mCurrentUser.getInServiceOrderList(0, 20, Order.ORDERSTATUS_READYED, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				try {
					JSONArray array=object.getJSONArray("result");
					int i=0;
					while(i<array.length()){
						Order order=Order.parse(array.getJSONObject(i));
						mOrders.add(order);
						i++;
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				mAdapter=new OrderAdapter(InServiceActivity.this, mOrders);
				mXlv.setAdapter(mAdapter);
			}
			
			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
				toast(error);
			}
		});
	}

	@Override
	protected void setListeners() {
		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
//				startActivity(MineActivity.class);
				finish();
			}
		});
		
		mXlv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Order order=mOrders.get(arg2-1);
				Intent intent=null;
				//去支付
				if(order.orderStatus==Order.ORDERSTATUS_REACHED){
					intent=new Intent(InServiceActivity.this, PayActivity.class);
				}else{
					intent=new Intent(InServiceActivity.this, OrderDetailActivity.class);
				}
				intent.putExtra("order", order);
				startActivityForResult(intent,0x01);
			}
		});

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		mOrders.clear();
		getData();
	}

}
